home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / dev / cross / ava-0.2.5.lha / ava-0.2.5 / src / Global.h < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-23  |  3.4 KB  |  133 lines

  1. /*
  2.     Global.h
  3.     
  4.     Global Classes, Variables, Definitions ...
  5.     Uros Platise, 1998
  6. */
  7.  
  8. #ifndef __Global
  9. #define __Global
  10.  
  11. #include <stdio.h>
  12. #include <assert.h>
  13.  
  14. /*
  15.   Global Settings 
  16.    * full path to main architecture file and 
  17.    * default include directory
  18.   AVA_BASE_LIB can be overriden externally.
  19. */
  20. #ifndef AVA_LIB
  21. #  define AVA_LIB        "/usr/local/uTools/ava"
  22. #endif
  23.  
  24. /* 
  25.    arch.inc is seeked in the AVA_BASE_LIB directory 
  26. */
  27. #define AVA_ARCH        "arch.inc"
  28. #define AVA_TARGET        "target.inc"
  29. #define AVA_NULL_DEVICE        "/dev/null"
  30.  
  31. /* 
  32.   Smart Pointer Class 
  33. */
  34. template <class TRec>
  35. class TPt{
  36. private:
  37.   TRec* Addr;
  38.   void MkRef(){if (Addr!=NULL){Addr->CRef++;}}
  39.   void UnRef(){if (Addr!=NULL){Addr->CRef--;if (Addr->CRef==0){delete Addr;}}}
  40. public:
  41.   TPt():Addr(NULL){}
  42.   TPt(TRec* _Addr): Addr(_Addr){MkRef();}
  43.   TPt(const TPt& Pt): Addr(Pt.Addr){MkRef();}
  44.   ~TPt(){UnRef();}
  45.  
  46.   TPt& operator=(const TPt& Pt){
  47.     if (this!=&Pt){UnRef(); Addr=Pt.Addr; MkRef();} return *this;}
  48.   TPt& operator=(TRec* _Addr){
  49.     if (Addr!=_Addr){UnRef();Addr=_Addr;MkRef();} return *this;}
  50.   bool operator==(const TPt& Pt) const {return Addr==Pt.Addr;}
  51.  
  52.   TRec* operator->() const {assert(Addr!=NULL); return Addr;}
  53.   TRec& operator*() const {assert(Addr!=NULL); return *Addr;}
  54.   TRec& operator[](int RecN) const {assert(Addr!=NULL); return Addr[RecN];}
  55.   TRec* operator()() const {return Addr;}
  56.   
  57.   /* think once more! */
  58.   bool operator<(const TPt& Pt){return Addr < Pt.Addr;}
  59. };
  60.  
  61. /* 
  62.   Target - Architecture Virtual Class 
  63. */
  64. class TArch {
  65. public:
  66.   virtual char* Device(char* buf)=0; /* tell device ID for obj... 
  67.                                    (should be the same as in arch.inc) */
  68.   virtual void IsSameDevice()=0;                   
  69.   virtual int is_Arch()=0;    /* returns non-zero if current token is
  70.                                    a part of architecture reserved words. */                   
  71.   virtual void Parse()=0;    /* Parse assembler instructions */
  72.   virtual void Translate(int instruction)=0;
  73.   virtual ~TArch(){}
  74.   TArch():CRef(0){}  
  75. private:
  76.   int CRef;
  77. public:
  78.   friend TPt<TArch>;
  79. };
  80.  
  81. typedef TPt<TArch> PArch;
  82. extern PArch archp;
  83.  
  84. /*
  85.   Fixed Stack
  86.   
  87.   Vector realisation with STD:
  88.     stack<bool, vector<bool> >
  89. */
  90. template<class Container>
  91. class TMicroStack{
  92. private:
  93.   Container* cp;
  94.   long idx, _size;
  95. public:
  96.   TMicroStack(int fixed_length=128):idx(0){
  97.     assert(fixed_length>=0); cp=new Container[_size=fixed_length];}
  98.   ~TMicroStack(){delete[] cp;}
  99.     
  100.   inline void push(const Container& x){assert(idx<_size); cp[idx++]=x;}
  101.   inline Container& pop(){assert(idx>0); return cp[--idx];}
  102.   void clear(){idx=0;}
  103.   Container operator[](int RecN) const {
  104.     assert(RecN>=0&&RecN<_size);return cp[RecN];}
  105.   
  106.   inline long capacity(){return idx;}
  107.   inline long size(){return idx;}
  108.   inline bool empty(){return idx==0;}
  109.   inline bool full(){return idx==_size;}
  110.   inline Container& top(){assert(idx>0);return cp[idx-1];}
  111.   inline Container& bottom(){assert(idx>0);return cp[0];}
  112.   
  113.   /* special non-standard functions */
  114.   void rotateUp(){
  115.     assert(idx>0);
  116.     Container buf = cp[idx-1];
  117.     for (int i=idx-1; i>0; i--){cp[i]=cp[i-1];}
  118.     cp[0]=buf;
  119.   }  
  120.   void sort(){
  121.     bool update=true;
  122.     while(update==true){
  123.       update=false; for(int i=0; i<(idx-1); i++){
  124.         if (cp[i+1]<cp[i]){      
  125.       Container buf=cp[i]; cp[i]=cp[i+1]; cp[i+1]=buf; update=true;}}}}
  126. };
  127.  
  128. /* AVA General Information */
  129. extern const char* version;
  130.  
  131. #endif
  132.  
  133.